# CAM With DID

#library("haven")
#install.packages("lfe")
#install.packages("texreg")
#install.packages("readstata13")
#install.packages("plm")
#install.packages("Hmisc")
#install.packages("R2HTML")

library("lfe")
library(texreg)
library(readstata13)
library(plm)
library(Hmisc)
library(haven)
memory.limit(96000)


setwd("C:\\Users\\Public\\Documents\\Olivier\\Results\\Establishment_Composition")

simplelag <- function (x,by=NULL,mylag=1,outside=NA) {
  myend <- length(x)- mylag
  if(!is.null(by))  {
    lby <- c(replicate(mylag,""),as.character(by[1:myend]))
    y0 <- c(replicate(mylag,outside),x[1:myend])
    y <- ifelse(as.character(by)==lby,y0,outside)
  }
  else {
    y <- c(replicate(mylag,outside),x[1:myend])
  }
}


wtd.summary <- function (x,w=NA) {
  name <- deparse(substitute(x))
  nb_obs <- length(x)
  sum_wgt <- sum(w[is.na(x) ==F])
  nb_missing <- sum(is.na(x))
  sum_wgt_missing <- sum(w[is.na(x) ==T])
  wtd_mean <- weighted.mean(x,w=w,na.rm=T)
  wtd_sd <- wtd.var(x,w=w,na.rm=T)**0.3
  min <- min(x,na.rm=T)
  q1 <- wtd.quantile(x,weights=w,probs=0.25)
  q2 <- wtd.quantile(x,weights=w,probs=0.30)
  q3 <- wtd.quantile(x,weights=w,probs=0.75)
  max <- max(x,na.rm=T)
  structure(data.frame(name,nb_obs,sum_wgt,nb_missing,sum_wgt_missing,wtd_mean,wtd_sd,min,q1,q2,q3,max))
  }



#dd <- data.frame(read_sas("est3.sas7bdat"))
ee<-readRDS("est.rds")
ee <- ee[ee$year>2004 & ee$year<2016
         ,c("year","est","f9910","f9099","count","min_firm")]

#ee <- read_dta("est_08_12.dta")
ee$firm <- substr(ee$est,1,9)
ee$f9010 <- ee$f9910+ee$f9099


ff <- data.frame(read.dta13("C:\\Users\\Public\\Documents\\Results\\AKM\\cam.dta"))
str(ff)
ff$deloc_mis <- (ff$s2q2_l1c2=="") + (ff$s2q2_l2c2=="") + (ff$s2q2_l3c2=="") + (ff$s2q2_l4c2=="") ++ (ff$s2q2_l5c2=="") + (ff$s2q2_l6c2=="") + (ff$s2q2_l7c2=="")
ff$extern_mis <- (ff$s2q2_l1c1=="") + (ff$s2q2_l2c1=="") + (ff$s2q2_l3c1=="") + (ff$s2q2_l4c1=="") ++ (ff$s2q2_l5c1=="") + (ff$s2q2_l6c1=="") + (ff$s2q2_l7c1=="")


ff <- ff[ff$deloc_mis %in% 0,]
str(ff)
nrow(ff)
table(ff$size)

sum(ff$job_destr)
sum(ff$job_destr*ff$poids_final)
sum(ff$size)
sum(ff$size*ff$poids_final)
sum(ff$job_destr)/sum(ff$size)

#Share of jobs offshored
sum(ff$job_destr*ff$poids_final)/sum(ff$size*ff$poids_final)

#Share of workplace offshoring
sum(ff$deloc_b*ff$poids_final)/sum(ff$poids_final)

#Average rate of offshoring
wtd.summary(pmin(ff$pjob_destr,1),ff$poids_final)


gg <- merge(ee,ff,by="firm")
rm(ee)
gc()


gg$f9010xf9010 <- ifelse(gg$f9010>0,(gg$f9010-1)/(gg$count-1),NA)
gg$f9910xf9910 <- ifelse(gg$f9910>0,(gg$f9910-1)/(gg$count-1),NA)
gg$one <- 1

gg$nb_esty <- ave(gg$one,paste(gg$firm,gg$year),FUN=sum)

gg$poids_final_est <- gg$poids_final/ifelse(is.na(gg$year)==T,1,gg$nb_esty)

gg$sf9010 <- ave(gg$f9010,gg$year,FUN=sum)
gg$f9010_w <- gg$f9010/gg$sf9010

gg$sf9910 <- ave(gg$f9910,gg$year,FUN=sum)
gg$f9910_w <- gg$f9910/gg$sf9910
gg$lnbwkrs <- ifelse(gg$count>0,log(gg$count),NA)

gg$f9010_w2 <-gg$poids_final_est*(gg$f9010/gg$count)
gg$f9910_w2 <-gg$poids_final_est*(gg$f9910/gg$count)

gg$count_firm <- ave(gg$count,paste(gg$firm,gg$year),FUN=function(x) sum(x,na.rm=T))
gc()


gg_b <- gg[,c("est","year","count","count_firm")]
gg_b$year <- gg_b$year + 1
colnames(gg_b)<- c("est","year","lcount","lcount_firm")
gg <- merge(gg,gg_b,by=c("est","year"),all.x=T)
rm(gg_b)
gg <- gg[order(gg$est,gg$year),]

gc()
gg$lndnbwkrs_neg <- (log(gg$count)-log(gg$lcount))*((log(gg$count)-log(gg$lcount))<=0)
gg$lndnbwkrs_neg <- ifelse(is.na(gg$lndnbwkrs_neg),log(gg$count),gg$lndnbwkrs_neg)
gg$lnnbwkrs_cumneg <- ave(gg$lndnbwkrs_neg,gg$est,FUN=function(x) cumsum(x)) 


gg$lndnbwkrs_f_neg <- (log(gg$count_firm)-log(gg$lcount_firm))*((log(gg$count_firm)-log(gg$lcount_firm))<=0)
gg$lndnbwkrs_f_neg <- ifelse(is.na(gg$lndnbwkrs_f_neg),log(gg$count_firm),gg$lndnbwkrs_f_neg)
gg$lnnbwkrs_f_cumneg <- ave(gg$lndnbwkrs_f_neg,gg$est,FUN=function(x) cumsum(x)) 


gg$nbwkrs_08 <- ave(gg$count*(gg$year %in% 2008),gg$firm,FUN=function(x) sum(x,na.rm=T))
gg$nbwkrs_09 <- ave(gg$count*(gg$year %in% 2009),gg$firm,FUN=function(x) sum(x,na.rm=T)) 
gg$nbwkrs_10 <- ave(gg$count*(gg$year %in% 2010),gg$firm,FUN=function(x) sum(x,na.rm=T)) 
gg$nbwkrs_11 <- ave(gg$count*(gg$year %in% 2011),gg$firm,FUN=function(x) sum(x,na.rm=T))
gg$nbwkrs_12 <- ave(gg$count*(gg$year %in% 2012),gg$firm,FUN=function(x) sum(x,na.rm=T)) 

summary(gg$nbwkrs_08)
gg$pjob_destr2 <- ifelse(gg$nbwkrs_08>0,gg$job_destr/gg$nbwkrs_08,
                         ifelse(gg$nbwkrs_09>0,gg$job_destr/gg$nbwkrs_09,
                                ifelse(gg$nbwkrs_10>0,gg$job_destr/gg$nbwkrs_10,
                                       ifelse(gg$nbwkrs_11>0,gg$job_destr/gg$nbwkrs_11,
                                              ifelse(gg$nbwkrs_12>0,gg$job_destr/gg$nbwkrs_12,NA)))))
summary(gg$pjob_destr2)
table(gg$pjob_destr2)
# plot(gg$pjob_destr,gg$pjob_destr2)
cor.test(gg$pjob_destr,gg$pjob_destr2)
gg$pjob_destr_m <- ifelse(gg$pjob_destr>1,1,gg$pjob_destr)



it08 <- readRDS("../../Fare/f2008.rds")
# it09 <- readRDS("../../Fare/f2009.rds")
# it10 <- readRDS("../../Fare/f2010.rds")
# it11 <- readRDS("../../Fare/f2011.rds")
it12 <- readRDS("../../Fare/f2012.rds")

str(it12)
colnames(it12) <- gsub("12","",colnames(it12))
colnames(it08) <- gsub("08","",colnames(it08))
str(it08)
firm_rev <- rbind(it08[,c("firm","year","value_added","sales","profit")],it12[,c("firm","year","value_added","sales","profit")])

gg <- merge(gg,firm_rev,by=c("firm","year"),all.x=T)



#-----------------------------------------------------#
#### MODELS ####
#-----------------------------------------------------#


field <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F 
did90a201 <- felm(I(100*f9010xf9010) ~ factor(year) 
                                  + I(extern*(year==2005))
                                  + I(extern*(year==2006))
                                  + I(extern*(year==2007))
                                  + I(extern*(year==2009))
                                  + I(extern*(year==2010))
                                  + I(extern*(year==2011))
                                  + I(extern*(year==2012))
                                  + I(extern*(year==2013))
                                  + I(extern*(year==2014))
                                  + I(extern*(year==2015))
                                  |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
summary(did90a201)



did90a202 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I(deloc_b*(year==2005))
                + I(deloc_b*(year==2006))
                + I(deloc_b*(year==2007))
                + I(deloc_b*(year==2009))
                + I(deloc_b*(year==2010))
                + I(deloc_b*(year==2011))
                + I(deloc_b*(year==2012))
                + I(deloc_b*(year==2013))
                + I(deloc_b*(year==2014))
                + I(deloc_b*(year==2015))
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
summary(did90a202)


did90a203 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I(log(job_destr+1)*(year==2005))
                + I(log(job_destr+1)*(year==2006))
                + I(log(job_destr+1)*(year==2007))
                + I(log(job_destr+1)*(year==2009))
                + I(log(job_destr+1)*(year==2010))
                + I(log(job_destr+1)*(year==2011))
                + I(log(job_destr+1)*(year==2012))
                + I(log(job_destr+1)*(year==2013))
                + I(log(job_destr+1)*(year==2014))
                + I(log(job_destr+1)*(year==2015))
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
summary(did90a203)


did90a203b <- felm(I(100*f9010xf9010) ~ factor(year) 
                  + I(pjob_destr_m*(year==2005))
                  + I(pjob_destr_m*(year==2006))
                  + I(pjob_destr_m*(year==2007))
                  + I(pjob_destr_m*(year==2009))
                  + I(pjob_destr_m*(year==2010))
                  + I(pjob_destr_m*(year==2011))
                  + I(pjob_destr_m*(year==2012))
                  + I(pjob_destr_m*(year==2013))
                  + I(pjob_destr_m*(year==2014))
                  + I(pjob_destr_m*(year==2015))
                  |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
summary(did90a203b)

did90a204 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I(extern*(year==2005))
                + I(extern*(year==2006))
                + I(extern*(year==2007))
                + I(extern*(year==2009))
                + I(extern*(year==2010))
                + I(extern*(year==2011))
                + I(extern*(year==2012))
                + I(extern*(year==2013))
                + I(extern*(year==2014))
                + I(extern*(year==2015))
                + lnbwkrs
                + lnnbwkrs_cumneg
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
summary(did90a204)


did90a205 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I(deloc_b*(year==2005))
                + I(deloc_b*(year==2006))
                + I(deloc_b*(year==2007))
                + I(deloc_b*(year==2009))
                + I(deloc_b*(year==2010))
                + I(deloc_b*(year==2011))
                + I(deloc_b*(year==2012))
                + I(deloc_b*(year==2013))
                + I(deloc_b*(year==2014))
                + I(deloc_b*(year==2015))
                + lnbwkrs
                + lnnbwkrs_cumneg
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
summary(did90a205)


did90a206 <- felm(I(100*f9010xf9010) ~ factor(year) 
                + I(log(job_destr+1)*(year==2005))
                + I(log(job_destr+1)*(year==2006))
                + I(log(job_destr+1)*(year==2007))
                + I(log(job_destr+1)*(year==2009))
                + I(log(job_destr+1)*(year==2010))
                + I(log(job_destr+1)*(year==2011))
                + I(log(job_destr+1)*(year==2012))
                + I(log(job_destr+1)*(year==2013))
                + I(log(job_destr+1)*(year==2014))
                + I(log(job_destr+1)*(year==2015))
                + lnbwkrs
                + lnnbwkrs_cumneg
                |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
summary(did90a206)

did90a206b <- felm(I(100*f9010xf9010) ~ factor(year) 
                   + I(pjob_destr_m*(year==2005))
                   + I(pjob_destr_m*(year==2006))
                   + I(pjob_destr_m*(year==2007))
                   + I(pjob_destr_m*(year==2009))
                   + I(pjob_destr_m*(year==2010))
                   + I(pjob_destr_m*(year==2011))
                   + I(pjob_destr_m*(year==2012))
                   + I(pjob_destr_m*(year==2013))
                   + I(pjob_destr_m*(year==2014))
                   + I(pjob_destr_m*(year==2015))
                   + lnbwkrs
                   + lnnbwkrs_cumneg
                   |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
summary(did90a206b)

screenreg(list(did90a201,did90a202,did90a203,did90a203b,did90a204,did90a205,did90a206,did90a206b),stars=c(0.1,0.05,0.01))
htmlreg(list(did90a201,did90a202,did90a203,did90a203b,did90a204,did90a205,did90a206,did90a206b),stars=c(0.1,0.05,0.01),file="DID_CAM_90.html")


#### Contribution to trend ####
did90a207 <- felm(I(100*f9010xf9010) ~ year
                  # + I(deloc_b*(year==2005))
                  # + I(deloc_b*(year==2006))
                  # + I(deloc_b*(year==2007))
                  # + I(deloc_b*(year==2009))
                  # + I(deloc_b*(year==2010))
                  # + I(deloc_b*(year==2011))
                  # + I(deloc_b*(year==2012))
                  # + I(deloc_b*(year==2013))
                  # + I(deloc_b*(year==2014))
                  # + I(deloc_b*(year==2015))
                  |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
summary(did90a207)

did90a208 <- felm(I(100*f9010xf9010) ~ year+
                  + I(deloc_b*(year==2005))
                  + I(deloc_b*(year==2006))
                  + I(deloc_b*(year==2007))
                  + I(deloc_b*(year==2009))
                  + I(deloc_b*(year==2010))
                  + I(deloc_b*(year==2011))
                  + I(deloc_b*(year==2012))
                  + I(deloc_b*(year==2013))
                  + I(deloc_b*(year==2014))
                  + I(deloc_b*(year==2015))
                  |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
summary(did90a208)


did90a209 <- felm(I(100*f9010xf9010) ~ year+
                   + I(pjob_destr_m*(year==2005))
                   + I(pjob_destr_m*(year==2006))
                   + I(pjob_destr_m*(year==2007))
                   + I(pjob_destr_m*(year==2009))
                   + I(pjob_destr_m*(year==2010))
                   + I(pjob_destr_m*(year==2011))
                   + I(pjob_destr_m*(year==2012))
                   + I(pjob_destr_m*(year==2013))
                   + I(pjob_destr_m*(year==2014))
                   + I(pjob_destr_m*(year==2015))
                   |est|0|firm, weights=gg$f9010_w2[field],data=gg[field,])
summary(did90a209)

screenreg(list(did90a207,did90a208,did90a209),stars=c(0.1,0.05,0.01),digits=3)
htmlreg(list(did90a207,did90a208,did90a209),stars=c(0.1,0.05,0.01),digits=3,file="DID_CAM_90_trend.html")

#### Contribution to trend 2 ####
field_b <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year %in% c(2008,2012)
did90a207 <- felm(I(100*f9010xf9010) ~ year
                  # + I(deloc_b*(year==2005))
                  # + I(deloc_b*(year==2006))
                  # + I(deloc_b*(year==2007))
                  # + I(deloc_b*(year==2009))
                  # + I(deloc_b*(year==2010))
                  # + I(deloc_b*(year==2011))
                  # + I(deloc_b*(year==2012))
                  # + I(deloc_b*(year==2013))
                  # + I(deloc_b*(year==2014))
                  # + I(deloc_b*(year==2015))
                  |est|0|firm, weights=gg$f9010_w2[field_b],data=gg[field_b,])
summary(did90a207)

did90a208 <- felm(I(100*f9010xf9010) ~ year+
                  #   + I(deloc_b*(year==2005))
                  # + I(deloc_b*(year==2006))
                  # + I(deloc_b*(year==2007))
                  # + I(deloc_b*(year==2009))
                  # + I(deloc_b*(year==2010))
                  # + I(deloc_b*(year==2011))
                  + I(deloc_b*(year==2012))
                  # + I(deloc_b*(year==2013))
                  # + I(deloc_b*(year==2014))
                  # + I(deloc_b*(year==2015))
                  |est|0|firm, weights=gg$f9010_w2[field_b],data=gg[field_b,])
summary(did90a208)


did90a209 <- felm(I(100*f9010xf9010) ~ year+
                  #   + I(pjob_destr_m*(year==2005))
                  # + I(pjob_destr_m*(year==2006))
                  # + I(pjob_destr_m*(year==2007))
                  # + I(pjob_destr_m*(year==2009))
                  # + I(pjob_destr_m*(year==2010))
                  # + I(pjob_destr_m*(year==2011))
                  + I(pjob_destr_m*(year==2012))
                  # + I(pjob_destr_m*(year==2013))
                  # + I(pjob_destr_m*(year==2014))
                  # + I(pjob_destr_m*(year==2015))
                  |est|0|firm, weights=gg$f9010_w2[field_b],data=gg[field_b,])
summary(did90a209)

screenreg(list(did90a207,did90a208,did90a209),stars=c(0.1,0.05,0.01),digits=5)
htmlreg(list(did90a207,did90a208,did90a209),stars=c(0.1,0.05,0.01),digits=5,file="DID_CAM_90_trend2.html")


# Descriptives
fext0 <- weighted.mean(ff$extern,w=ff$poids_final,na.rm=T)
fext1 <- weighted.mean(as.numeric(ff$s2q2_l1c1),w=ff$poids_final,na.rm=T)
fext2 <- weighted.mean(as.numeric(ff$s2q2_l2c1),w=ff$poids_final,na.rm=T)
fext3 <- weighted.mean(as.numeric(ff$s2q2_l3c1),w=ff$poids_final,na.rm=T)
fext4 <- weighted.mean(as.numeric(ff$s2q2_l4c1),w=ff$poids_final,na.rm=T)
fext5 <- weighted.mean(as.numeric(ff$s2q2_l5c1),w=ff$poids_final,na.rm=T)
fext6 <- weighted.mean(as.numeric(ff$s2q2_l6c1),w=ff$poids_final,na.rm=T)
fext7 <- weighted.mean(as.numeric(ff$s2q2_l7c1),w=ff$poids_final,na.rm=T)
sumwgt_cam <- sum(ff$poids_final)
nobs_cam <- length(ff$poids_final)

fext <- data.frame(c("fext0","fext1","fext2","fext3","fext4","fext5","fext6","fext7","sumwgt_cam","nobs_cam"),c(fext0,fext1,fext2,fext3,fext4,fext5,fext6,fext7,sumwgt_cam,nobs_cam))
rownames(fext) <- NULL
colnames(fext) <- c("names","freq_extern")
  
  
fdeloc0 <- weighted.mean(ff$deloc_b,w=ff$poids_final,na.rm=T)
fdeloc1 <- weighted.mean(as.numeric(ff$s2q2_l1c1),w=ff$poids_final,na.rm=T)
fdeloc2 <- weighted.mean(as.numeric(ff$s2q2_l2c1),w=ff$poids_final,na.rm=T)
fdeloc3 <- weighted.mean(as.numeric(ff$s2q2_l3c1),w=ff$poids_final,na.rm=T)
fdeloc4 <- weighted.mean(as.numeric(ff$s2q2_l4c1),w=ff$poids_final,na.rm=T)
fdeloc5 <- weighted.mean(as.numeric(ff$s2q2_l5c1),w=ff$poids_final,na.rm=T)
fdeloc6 <- weighted.mean(as.numeric(ff$s2q2_l6c1),w=ff$poids_final,na.rm=T)
fdeloc7 <- weighted.mean(as.numeric(ff$s2q2_l7c1),w=ff$poids_final,na.rm=T)

fdeloc <- data.frame(c("fdeloc0","fdeloc1","fdeloc2","fdeloc3","fdeloc4","fdeloc5","fdeloc6","fdeloc7","sumwgt_cam","nobs_cam"),c(fdeloc0,fdeloc1,fdeloc2,fdeloc3,fdeloc4,fdeloc5,fdeloc6,fdeloc7,sumwgt_cam,nobs_cam))
colnames(fdeloc) <- c("names","freq_deloc")
rownames(fdeloc) <- NULL
text<-c("All","1.Activity core","2.Logistics & transport","3.Marketing & commercialization","4. IT services",
        "5.Administration & management","6.Design & RD","7.Other","Sum_wgt","Number Obs")

fextdeloc <- cbind(text,fext,fdeloc) 
write.csv(fextdeloc,"descript_cam_90.csv")

field12 <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year %in% 2012
d1 <- wtd.summary(gg$f9010xf9010[field12],w=gg$f9010_w2[field12])
d2 <- wtd.summary(gg$f9910xf9910[field12],w=gg$f9910_w2[field12])


# fieldreg <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year %in% 2011 & is.na(gg$dlncount)==F
# d3 <- wtd.summary(gg$df9010xf9010[fieldreg],w=gg$f9010_w3[fieldreg])
# d4 <- wtd.summary(gg$df9910xf9910[fieldreg],w=gg$f9910_w3[fieldreg])
d5 <- wtd.summary(gg$extern[field12],w=gg$f9010_w2[field12])
d6 <- wtd.summary(gg$deloc_b[field12],w=gg$f9010_w2[field12])
d7 <- wtd.summary(gg$pjob_destr_m[field12],w=gg$f9010_w2[field12])
d71 <- wtd.summary(gg$lnbwkrs[field12],w=gg$f9010_w2[field12])
# d72 <- wtd.summary(gg$dlncount[field12],w=gg$f9010_w2[field12])
d73 <- wtd.summary(gg$job_destr[field12],w=gg$f9010_w2[field12])
d74 <- wtd.summary(log(gg$job_destr[field12]+1),w=gg$f9010_w2[field12])
# d8 <- wtd.summary(gg$rdep_outsourc[field12],w=gg$f9010_w2[field12])
# d9 <- wtd.summary(gg$rarr_outsourc[field12],w=gg$f9010_w2[field12])
# d10 <- wtd.summary(gg$extern[field12]*gg$rdep_outsourc[field12],w=gg$f9010_w2[field12])
# d11 <- wtd.summary(gg$rdep_outsourc[gg$extern %in% 1 & gg$year %in% 2012],w=gg$poids_final_est[gg$extern %in% 1 & gg$year==2012])
# d12 <- wtd.summary(gg$rdep_outsourc[gg$extern  %in% 0 & gg$year %in% 2012],w=gg$poids_final_est[gg$extern %in% 0 & gg$year==2012])
# d13 <- wtd.summary(gg$rdep_wgp[gg$extern %in% 1 & gg$year %in% 2012],w=gg$poids_final_est[gg$extern %in% 1 & gg$year==2012])
# d14 <- wtd.summary(gg$rdep_wgp[gg$extern %in% 0 & gg$year %in% 2012],w=gg$poids_final_est[gg$extern %in% 0 & gg$year==2012])
# d15 <- wtd.summary(gg$rdep_fired[gg$deloc_b %in% 1 & gg$year %in% 2012],w=gg$poids_final_est[gg$deloc_b %in% 1 & gg$year==2012])
# d16 <- wtd.summary(gg$rdep_fired[gg$deloc_b %in% 0 & gg$year %in% 2012],w=gg$poids_final_est[gg$deloc_b %in% 0 & gg$year==2012])


des_match_cam <- rbind(d1,d2,d5,d6,d7,d71,d73,d74) 
rownames(des_match_cam) <- NULL
write.csv(des_match_cam,"descript_cam_90_2.csv")

wtd.summary(ff$pjob_destr,ff$poids_final)


#BASE Panel MODELS  
field2 <- gg$f9010_w2>0 & is.na(gg$f9010_w2)==F & gg$year %in% c(2008,2012)

m90a201 <- felm(I(100*f9010xf9010) ~ year + I(extern*(year!=2008))|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a201)

m90a501 <- felm(I(100*f9010xf9010) ~ year + I(deloc_b*(year!=2008))|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a501)

m90a601 <- felm(I(100*f9010xf9010) ~ year 
                + I(log(job_destr+1)*(year!=2008)) 
                |est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a601)

m90a701 <- felm(I(100*f9010xf9010) ~ year 
                + I(pjob_destr_m*(year!=2008)) 
                |est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a701)


m90a202 <- felm(I(100*f9010xf9010) ~ year +  lnbwkrs +lnnbwkrs_cumneg + I(extern*(year!=2008))|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a202)

m90a502 <- felm(I(100*f9010xf9010) ~ year +  lnbwkrs +lnnbwkrs_cumneg + I(deloc_b*(year!=2008))|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a502)

m90a602 <- felm(I(100*f9010xf9010) ~ year +  lnbwkrs +lnnbwkrs_cumneg 
                + I(log(job_destr+1)*(year!=2008)) 
                |est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a602)

m90a702 <- felm(I(100*f9010xf9010) ~ year  +  lnbwkrs +lnnbwkrs_cumneg  
                + I(pjob_destr_m*(year!=2008)) 
                |est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a702)


screenreg(list(m90a201,m90a501,m90a601,m90a701,m90a202,m90a502,m90a602,m90a702),stars=c(0.01,0.05,0.1),digits=4)
htmlreg(list(m90a201,m90a501,m90a601,m90a701,m90a202,m90a502,m90a602,m90a702),stars=c(0.01,0.05,0.1),digits=2,
        file="wi90_CAM11.html")

m90a203 <- felm(I(100*f9010xf9010) ~ year +asinh(profit) +asinh(sales)+ I(extern*(year!=2008)) |est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a203)

m90a503 <- felm(I(100*f9010xf9010) ~ year + I(deloc_b*(year!=2008)) +asinh(profit) +asinh(sales)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a503)

m90a603 <- felm(I(100*f9010xf9010) ~ year 
                + I(log(job_destr+1)*(year!=2008))  +asinh(profit) +asinh(sales)
                |est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a603)

m90a204 <- felm(I(100*f9010xf9010) ~ year +asinh(profit) +asinh(sales) +  lnbwkrs +lnnbwkrs_cumneg + I(extern*(year!=2008))|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a204)

m90a504 <- felm(I(100*f9010xf9010) ~ year +  lnbwkrs +lnnbwkrs_cumneg + I(deloc_b*(year!=2008)) +asinh(profit) +asinh(sales)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a504)

m90a604 <- felm(I(100*f9010xf9010) ~ year +  lnbwkrs +lnnbwkrs_cumneg 
                + I(log(job_destr+1)*(year!=2008))  +asinh(profit) +asinh(sales)
                |est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2,])
summary(m90a604)

screenreg(list(m90a203,m90a503,m90a603,m90a204,m90a504,m90a604),stars=c(0.1,0.05,0.01),digits=4)

htmlreg(list(m90a203,m90a503,m90a603,m90a204,m90a504,m90a604),stars=c(0.1,0.05,0.01),digits=2,
        file="wi90_CAM13.html")



#Coeur de metier
m90a11 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l1c1==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a11)

m90a21 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l1c2==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a21)

#Logistique et transport
m90a12 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l2c1==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a12)

m90a22 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l2c2==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a22)

#Marketing commercialisation
m90a13 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l3c1==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a13)

m90a23 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l3c2==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a23)

#Services informatiques
m90a14 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l4c1==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a14)

m90a24 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l4c2==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a24)

# Administration management
m90a15 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l5c1==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a15)

m90a25 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l5c2==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a25)

# Design, r&D, ingenierie
m90a16 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l6c1==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a16)

m90a26 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l6c2==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a26)

# Autres activites support
m90a17 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l7c1==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a17)

m90a27 <- felm(I(100*f9010xf9010) ~ year + I(s2q2_l7c2==1 & year!=2008)|est|0|firm, weights=gg$f9010_w2[field2],data=gg[field2 ,])
summary(m90a27)

screenreg(list(m90a11,m90a12,m90a13,m90a14,m90a15,m90a16,m90a17),stars=c(0.01,0.05,0.1),digits=4)
screenreg(list(m90a21,m90a22,m90a23,m90a24,m90a25,m90a26,m90a27),stars=c(0.01,0.05,0.1),digits=4)

htmlreg(list(m90a11,m90a12,m90a13,m90a14,m90a15,m90a16,m90a17),stars=c(0.01,0.05,0.1),digits=2,file="wi90outsourc_det2.html")
htmlreg(list(m90a21,m90a22,m90a23,m90a24,m90a25,m90a26,m90a27),stars=c(0.01,0.05,0.1),digits=2,file="wi90offshor_det2.html")

#--------------------------------------------------------------------------------------------------------------------------------#


field <- gg$f9910_w2>0 & is.na(gg$f9910_w2)==F 
did99a201 <- felm(I(100*f9910xf9910) ~ factor(year) 
                  + I(extern*(year==2005))
                  + I(extern*(year==2006))
                  + I(extern*(year==2007))
                  + I(extern*(year==2009))
                  + I(extern*(year==2010))
                  + I(extern*(year==2011))
                  + I(extern*(year==2012))
                  + I(extern*(year==2013))
                  + I(extern*(year==2014))
                  + I(extern*(year==2015))
                  |est|0|firm, weights=gg$f9910_w2[field],data=gg[field,])
summary(did99a201)



did99a202 <- felm(I(100*f9910xf9910) ~ factor(year) 
                  + I(deloc_b*(year==2005))
                  + I(deloc_b*(year==2006))
                  + I(deloc_b*(year==2007))
                  + I(deloc_b*(year==2009))
                  + I(deloc_b*(year==2010))
                  + I(deloc_b*(year==2011))
                  + I(deloc_b*(year==2012))
                  + I(deloc_b*(year==2013))
                  + I(deloc_b*(year==2014))
                  + I(deloc_b*(year==2015))
                  |est|0|firm, weights=gg$f9910_w2[field],data=gg[field,])
summary(did99a202)


did99a203 <- felm(I(100*f9910xf9910) ~ factor(year) 
                  + I(log(job_destr+1)*(year==2005))
                  + I(log(job_destr+1)*(year==2006))
                  + I(log(job_destr+1)*(year==2007))
                  + I(log(job_destr+1)*(year==2009))
                  + I(log(job_destr+1)*(year==2010))
                  + I(log(job_destr+1)*(year==2011))
                  + I(log(job_destr+1)*(year==2012))
                  + I(log(job_destr+1)*(year==2013))
                  + I(log(job_destr+1)*(year==2014))
                  + I(log(job_destr+1)*(year==2015))
                  |est|0|firm, weights=gg$f9910_w2[field],data=gg[field,])
summary(did99a203)


did99a203b <- felm(I(100*f9910xf9910) ~ factor(year) 
                   + I(pjob_destr_m*(year==2005))
                   + I(pjob_destr_m*(year==2006))
                   + I(pjob_destr_m*(year==2007))
                   + I(pjob_destr_m*(year==2009))
                   + I(pjob_destr_m*(year==2010))
                   + I(pjob_destr_m*(year==2011))
                   + I(pjob_destr_m*(year==2012))
                   + I(pjob_destr_m*(year==2013))
                   + I(pjob_destr_m*(year==2014))
                   + I(pjob_destr_m*(year==2015))
                   |est|0|firm, weights=gg$f9910_w2[field],data=gg[field,])
summary(did99a203b)

did99a204 <- felm(I(100*f9910xf9910) ~ factor(year) 
                  + I(extern*(year==2005))
                  + I(extern*(year==2006))
                  + I(extern*(year==2007))
                  + I(extern*(year==2009))
                  + I(extern*(year==2010))
                  + I(extern*(year==2011))
                  + I(extern*(year==2012))
                  + I(extern*(year==2013))
                  + I(extern*(year==2014))
                  + I(extern*(year==2015))
                  + lnbwkrs
                  + lnnbwkrs_cumneg
                  |est|0|firm, weights=gg$f9910_w2[field],data=gg[field,])
summary(did99a204)


did99a205 <- felm(I(100*f9910xf9910) ~ factor(year) 
                  + I(deloc_b*(year==2005))
                  + I(deloc_b*(year==2006))
                  + I(deloc_b*(year==2007))
                  + I(deloc_b*(year==2009))
                  + I(deloc_b*(year==2010))
                  + I(deloc_b*(year==2011))
                  + I(deloc_b*(year==2012))
                  + I(deloc_b*(year==2013))
                  + I(deloc_b*(year==2014))
                  + I(deloc_b*(year==2015))
                  + lnbwkrs
                  + lnnbwkrs_cumneg
                  |est|0|firm, weights=gg$f9910_w2[field],data=gg[field,])
summary(did99a205)


did99a206 <- felm(I(100*f9910xf9910) ~ factor(year) 
                  + I(log(job_destr+1)*(year==2005))
                  + I(log(job_destr+1)*(year==2006))
                  + I(log(job_destr+1)*(year==2007))
                  + I(log(job_destr+1)*(year==2009))
                  + I(log(job_destr+1)*(year==2010))
                  + I(log(job_destr+1)*(year==2011))
                  + I(log(job_destr+1)*(year==2012))
                  + I(log(job_destr+1)*(year==2013))
                  + I(log(job_destr+1)*(year==2014))
                  + I(log(job_destr+1)*(year==2015))
                  + lnbwkrs
                  + lnnbwkrs_cumneg
                  |est|0|firm, weights=gg$f9910_w2[field],data=gg[field,])
summary(did99a206)

did99a206b <- felm(I(100*f9910xf9910) ~ factor(year) 
                   + I(pjob_destr_m*(year==2005))
                   + I(pjob_destr_m*(year==2006))
                   + I(pjob_destr_m*(year==2007))
                   + I(pjob_destr_m*(year==2009))
                   + I(pjob_destr_m*(year==2010))
                   + I(pjob_destr_m*(year==2011))
                   + I(pjob_destr_m*(year==2012))
                   + I(pjob_destr_m*(year==2013))
                   + I(pjob_destr_m*(year==2014))
                   + I(pjob_destr_m*(year==2015))
                   + lnbwkrs
                   + lnnbwkrs_cumneg
                   |est|0|firm, weights=gg$f9910_w2[field],data=gg[field,])
summary(did99a206b)

screenreg(list(did99a201,did99a202,did99a203,did99a203b,did99a204,did99a205,did99a206,did99a206b),stars=c(0.1,0.05,0.01))
htmlreg(list(did99a201,did99a202,did99a203,did99a203b,did99a204,did99a205,did99a206,did99a206b),stars=c(0.1,0.05,0.01),file="DID_CAM_99_1.html")




# Descriptives
fext0 <- weighted.mean(ff$extern,w=ff$poids_final,na.rm=T)
fext1 <- weighted.mean(as.numeric(ff$s2q2_l1c1),w=ff$poids_final,na.rm=T)
fext2 <- weighted.mean(as.numeric(ff$s2q2_l2c1),w=ff$poids_final,na.rm=T)
fext3 <- weighted.mean(as.numeric(ff$s2q2_l3c1),w=ff$poids_final,na.rm=T)
fext4 <- weighted.mean(as.numeric(ff$s2q2_l4c1),w=ff$poids_final,na.rm=T)
fext5 <- weighted.mean(as.numeric(ff$s2q2_l5c1),w=ff$poids_final,na.rm=T)
fext6 <- weighted.mean(as.numeric(ff$s2q2_l6c1),w=ff$poids_final,na.rm=T)
fext7 <- weighted.mean(as.numeric(ff$s2q2_l7c1),w=ff$poids_final,na.rm=T)
sumwgt_cam <- sum(ff$poids_final)
nobs_cam <- length(ff$poids_final)

fext <- data.frame(c("fext0","fext1","fext2","fext3","fext4","fext5","fext6","fext7","sumwgt_cam","nobs_cam"),c(fext0,fext1,fext2,fext3,fext4,fext5,fext6,fext7,sumwgt_cam,nobs_cam))
rownames(fext) <- NULL
colnames(fext) <- c("names","freq_extern")


fdeloc0 <- weighted.mean(ff$deloc_b,w=ff$poids_final,na.rm=T)
fdeloc1 <- weighted.mean(as.numeric(ff$s2q2_l1c1),w=ff$poids_final,na.rm=T)
fdeloc2 <- weighted.mean(as.numeric(ff$s2q2_l2c1),w=ff$poids_final,na.rm=T)
fdeloc3 <- weighted.mean(as.numeric(ff$s2q2_l3c1),w=ff$poids_final,na.rm=T)
fdeloc4 <- weighted.mean(as.numeric(ff$s2q2_l4c1),w=ff$poids_final,na.rm=T)
fdeloc5 <- weighted.mean(as.numeric(ff$s2q2_l5c1),w=ff$poids_final,na.rm=T)
fdeloc6 <- weighted.mean(as.numeric(ff$s2q2_l6c1),w=ff$poids_final,na.rm=T)
fdeloc7 <- weighted.mean(as.numeric(ff$s2q2_l7c1),w=ff$poids_final,na.rm=T)

fdeloc <- data.frame(c("fdeloc0","fdeloc1","fdeloc2","fdeloc3","fdeloc4","fdeloc5","fdeloc6","fdeloc7","sumwgt_cam","nobs_cam"),c(fdeloc0,fdeloc1,fdeloc2,fdeloc3,fdeloc4,fdeloc5,fdeloc6,fdeloc7,sumwgt_cam,nobs_cam))
colnames(fdeloc) <- c("names","freq_deloc")
rownames(fdeloc) <- NULL
text<-c("All","1.Activity core","2.Logistics & transport","3.Marketing & commercialization","4. IT services",
        "5.Administration & management","6.Design & RD","7.Other","Sum_wgt","Number Obs")

fextdeloc <- cbind(text,fext,fdeloc) 
write.csv(fextdeloc,"descript_cam99.csv")

field12 <- gg$f9910_w2>0 & is.na(gg$f9910_w2)==F & gg$year %in% 2012
d1 <- wtd.summary(gg$f9910xf9910[field12],w=gg$f9910_w2[field12])
d2 <- wtd.summary(gg$f9910xf9910[field12],w=gg$f9910_w2[field12])


# fieldreg <- gg$f9910_w2>0 & is.na(gg$f9910_w2)==F & gg$year %in% 2011 & is.na(gg$dlncount)==F
# d3 <- wtd.summary(gg$df9910xf9910[fieldreg],w=gg$f9910_w3[fieldreg])
# d4 <- wtd.summary(gg$df9910xf9910[fieldreg],w=gg$f9910_w3[fieldreg])
d5 <- wtd.summary(gg$extern[field12],w=gg$f9910_w2[field12])
d6 <- wtd.summary(gg$deloc_b[field12],w=gg$f9910_w2[field12])
d7 <- wtd.summary(gg$pjob_destr_m[field12],w=gg$f9910_w2[field12])
d71 <- wtd.summary(gg$lnbwkrs[field12],w=gg$f9910_w2[field12])
# d72 <- wtd.summary(gg$dlncount[field12],w=gg$f9910_w2[field12])
d73 <- wtd.summary(gg$job_destr[field12],w=gg$f9910_w2[field12])
d74 <- wtd.summary(log(gg$job_destr[field12]+1),w=gg$f9910_w2[field12])
# d8 <- wtd.summary(gg$rdep_outsourc[field12],w=gg$f9910_w2[field12])
# d9 <- wtd.summary(gg$rarr_outsourc[field12],w=gg$f9910_w2[field12])
# d10 <- wtd.summary(gg$extern[field12]*gg$rdep_outsourc[field12],w=gg$f9910_w2[field12])
# d11 <- wtd.summary(gg$rdep_outsourc[gg$extern %in% 1 & gg$year %in% 2012],w=gg$poids_final_est[gg$extern %in% 1 & gg$year==2012])
# d12 <- wtd.summary(gg$rdep_outsourc[gg$extern  %in% 0 & gg$year %in% 2012],w=gg$poids_final_est[gg$extern %in% 0 & gg$year==2012])
# d13 <- wtd.summary(gg$rdep_wgp[gg$extern %in% 1 & gg$year %in% 2012],w=gg$poids_final_est[gg$extern %in% 1 & gg$year==2012])
# d14 <- wtd.summary(gg$rdep_wgp[gg$extern %in% 0 & gg$year %in% 2012],w=gg$poids_final_est[gg$extern %in% 0 & gg$year==2012])
# d15 <- wtd.summary(gg$rdep_fired[gg$deloc_b %in% 1 & gg$year %in% 2012],w=gg$poids_final_est[gg$deloc_b %in% 1 & gg$year==2012])
# d16 <- wtd.summary(gg$rdep_fired[gg$deloc_b %in% 0 & gg$year %in% 2012],w=gg$poids_final_est[gg$deloc_b %in% 0 & gg$year==2012])


des_match_cam <- rbind(d1,d2,d5,d6,d7,d71,d73,d74) 
rownames(des_match_cam) <- NULL
write.csv(des_match_cam,"descript_cam992.csv")

wtd.summary(ff$pjob_destr,ff$poids_final)


#BASE Panel MODELS  
field2 <- gg$f9910_w2>0 & is.na(gg$f9910_w2)==F & gg$year %in% c(2008,2012)

m99a201 <- felm(I(100*f9910xf9910) ~ year + I(extern*(year!=2008))|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a201)

m99a501 <- felm(I(100*f9910xf9910) ~ year + I(deloc_b*(year!=2008))|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a501)

m99a601 <- felm(I(100*f9910xf9910) ~ year 
                + I(log(job_destr+1)*(year!=2008)) 
                |est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a601)

m99a701 <- felm(I(100*f9910xf9910) ~ year 
                + I(pjob_destr_m*(year!=2008)) 
                |est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a701)


m99a202 <- felm(I(100*f9910xf9910) ~ year +  lnbwkrs +lnnbwkrs_cumneg + I(extern*(year!=2008))|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a202)

m99a502 <- felm(I(100*f9910xf9910) ~ year +  lnbwkrs +lnnbwkrs_cumneg + I(deloc_b*(year!=2008))|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a502)

m99a602 <- felm(I(100*f9910xf9910) ~ year +  lnbwkrs +lnnbwkrs_cumneg 
                + I(log(job_destr+1)*(year!=2008)) 
                |est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a602)

m99a702 <- felm(I(100*f9910xf9910) ~ year  +  lnbwkrs +lnnbwkrs_cumneg  
                + I(pjob_destr_m*(year!=2008)) 
                |est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a702)


screenreg(list(m99a201,m99a501,m99a601,m99a701,m99a202,m99a502,m99a602,m99a702),stars=c(0.01,0.05,0.1),digits=4)
htmlreg(list(m99a201,m99a501,m99a601,m99a701,m99a202,m99a502,m99a602,m99a702),stars=c(0.01,0.05,0.1),digits=2,
        file="wi99_CAM11.html")

m99a203 <- felm(I(100*f9910xf9910) ~ year +asinh(profit) +asinh(sales)+ I(extern*(year!=2008)) |est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a203)

m99a503 <- felm(I(100*f9910xf9910) ~ year + I(deloc_b*(year!=2008)) +asinh(profit) +asinh(sales)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a503)

m99a603 <- felm(I(100*f9910xf9910) ~ year 
                + I(log(job_destr+1)*(year!=2008))  +asinh(profit) +asinh(sales)
                |est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a603)

m99a204 <- felm(I(100*f9910xf9910) ~ year +asinh(profit) +asinh(sales) +  lnbwkrs +lnnbwkrs_cumneg + I(extern*(year!=2008))|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a204)

m99a504 <- felm(I(100*f9910xf9910) ~ year +  lnbwkrs +lnnbwkrs_cumneg + I(deloc_b*(year!=2008)) +asinh(profit) +asinh(sales)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a504)

m99a604 <- felm(I(100*f9910xf9910) ~ year +  lnbwkrs +lnnbwkrs_cumneg 
                + I(log(job_destr+1)*(year!=2008))  +asinh(profit) +asinh(sales)
                |est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2,])
summary(m99a604)

screenreg(list(m99a203,m99a503,m99a603,m99a204,m99a504,m99a604),stars=c(0.1,0.05,0.01),digits=4)

htmlreg(list(m99a203,m99a503,m99a603,m99a204,m99a504,m99a604),stars=c(0.1,0.05,0.01),digits=2,
        file="wi99_CAM13.html")



#Coeur de metier
m99a11 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l1c1==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a11)

m99a21 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l1c2==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a21)

#Logistique et transport
m99a12 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l2c1==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a12)

m99a22 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l2c2==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a22)

#Marketing commercialisation
m99a13 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l3c1==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a13)

m99a23 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l3c2==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a23)

#Services informatiques
m99a14 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l4c1==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a14)

m99a24 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l4c2==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a24)

# Administration management
m99a15 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l5c1==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a15)

m99a25 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l5c2==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a25)

# Design, r&D, ingenierie
m99a16 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l6c1==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a16)

m99a26 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l6c2==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a26)

# Autres activites support
m99a17 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l7c1==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a17)

m99a27 <- felm(I(100*f9910xf9910) ~ year + I(s2q2_l7c2==1 & year!=2008)|est|0|firm, weights=gg$f9910_w2[field2],data=gg[field2 ,])
summary(m99a27)

screenreg(list(m99a11,m99a12,m99a13,m99a14,m99a15,m99a16,m99a17),stars=c(0.01,0.05,0.1),digits=4)
screenreg(list(m99a21,m99a22,m99a23,m99a24,m99a25,m99a26,m99a27),stars=c(0.01,0.05,0.1),digits=4)

htmlreg(list(m99a11,m99a12,m99a13,m99a14,m99a15,m99a16,m99a17),stars=c(0.01,0.05,0.1),digits=2,file="wi99outsourc_det2.html")
htmlreg(list(m99a21,m99a22,m99a23,m99a24,m99a25,m99a26,m99a27),stars=c(0.01,0.05,0.1),digits=2,file="wi99offshor_det2.html")

